﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GM.Forms
{
    public partial class MaterialMovementForm : Form
    {
        private ReportForm reportf = new ReportForm();
        private int movementtype = 0;

        public MaterialMovementForm()
        {
            InitializeComponent();
        }

        public void setType(int type)
        {
            movementtype = type;
        }

        private void MaterialMovementForm_Load(object sender, EventArgs e)
        {
            this.pracownikTableAdapter.Fill(this.evolutionDataSet.Pracownik);
            this.gm_materialyTableAdapter.Fill(this.evolutionDataSetmaterialy.gm_materialy);
            this.gm_materialy_ruchyTableAdapter.Fill(this.evolutionDataSetmaterialy.gm_materialy_ruchy);

            this.data_wydaniaDateTimePicker.MaxDate = DateTime.Now;
        }

        private void MaterialMovementForm_Shown(object sender, EventArgs e)
        {
 
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
           // gm_materialy_ruchyBindingNavigatorSaveItem.PerformClick();

            if (material_idComboBox.SelectedIndex >= 0 && odbiorca_idComboBox.SelectedIndex >= 0 && wydajacy_idComboBox.SelectedIndex >= 0)
            {
                this.Validate();
                this.gm_materialy_ruchyBindingSource.EndEdit();
                this.tableAdapterManager.UpdateAll(this.evolutionDataSetmaterialy);

                this.Hide();
                int id = ((evolutionDataSetmaterialy.gm_materialy_ruchyRow)((DataRowView)gm_materialy_ruchyBindingSource.Current).Row).id;
                GM.evolutionDataSetmaterialyTableAdapters.gm_materialy_ruchy_dokTableAdapter adapter = new evolutionDataSetmaterialyTableAdapters.gm_materialy_ruchy_dokTableAdapter();

                //  adapter.get .Fill(evolutionDataSetmaterialy.gm_materialy_ruchy_dok);
                //evolutionDataSetmaterialy.gm_materialy_ruchy_dok

                evolutionDataSetmaterialy.gm_materialy_ruchy_dokDataTable table = adapter.GetDataBy(id);

                GM.evolutionDataSetmaterialyTableAdapters.gm_materialyTableAdapter r_adapter = new evolutionDataSetmaterialyTableAdapters.gm_materialyTableAdapter();
                if (table.Count != 1) MessageBox.Show("Wystąpił błąd przy określaniu typu materiału.");
                else
                    switch (movementtype)
                    {
                        case 0:
                            reportf.Show("Materialy_PZ", table, "MaterialyDataSet1");
                            r_adapter.UpdateAddIlosc((int)table.Rows[0]["ilosc"], (int)table.Rows[0]["material_id"]);
                            break;
                        case 1:
                            evolutionDataSetmaterialy.gm_materialyDataTable mat_table = new GM.evolutionDataSetmaterialy.gm_materialyDataTable();
                            object i = r_adapter.GetDataById((int)table.Rows[0]["material_id"]).Rows[0]["ilosc"];
                            uint ilosc = (uint)i;
                            if (ilosc < (int)table.Rows[0]["ilosc"])
                            {
                                MessageBox.Show("Nie można wydać materiałów.");
                            }
                            else
                            {
                                reportf.Show("Materialy_RW", table, "MaterialyDataSet1");
                                r_adapter.UpdateAddIlosc(-(int)table.Rows[0]["ilosc"], (int)table.Rows[0]["material_id"]);
                            }
                            break;
                        case 2:
                            reportf.Show("Materialy_ZW", table, "MaterialyDataSet1");
                            r_adapter.UpdateAddIlosc((int)table.Rows[0]["ilosc"], (int)table.Rows[0]["material_id"]);
                            break;
                    }
            }
            else MessageBox.Show("Wszystkie pola formularza muszą być uzupełnione");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            gm_materialy_ruchyBindingSource.CancelEdit();
            this.Hide();
        }

        private void MaterialMovementForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            gm_materialy_ruchyBindingSource.CancelEdit();
            this.Hide();
        }

        private void gm_materialy_ruchyBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.gm_materialy_ruchyBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.evolutionDataSetmaterialy);

        }

        private void MaterialMovementForm_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                gm_materialy_ruchyBindingSource.AddNew();
                gm_materialy_ruchyBindingSource.MoveLast();

                ((evolutionDataSetmaterialy.gm_materialy_ruchyRow)((DataRowView)gm_materialy_ruchyBindingSource.Current).Row).data = DateTime.Now;
                ((evolutionDataSetmaterialy.gm_materialy_ruchyRow)((DataRowView)gm_materialy_ruchyBindingSource.Current).Row).rodzaj_ruchu = movementtype;
                gm_materialy_ruchyBindingSource.ResetBindings(false);

                switch (movementtype)
                {
                    case 0:
                        this.Text = "Przyjęcie na magazyn";
                        this.nr_fakturyTextBox.Show();
                        this.nr_fakturyLabel.Show();
                        break;
                    case 1:
                        this.Text = "Wydanie z magazynu";
                        this.nr_fakturyTextBox.Hide();
                        this.nr_fakturyLabel.Hide();
                        break;
                    case 2:
                        this.Text = "Zwrot na magazyn";
                        this.nr_fakturyTextBox.Hide();
                        this.nr_fakturyLabel.Hide();
                        break;
                }
            }
        }

    }
}
